package com.example.demo.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Goods;
import com.example.demo.entity.Mallgoods;
import com.example.demo.entity.Shoppingcart;
import com.example.demo.service.MallgoodsService;
import com.example.demo.service.ShoppingcartService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class ShoppingCarController {
    @Autowired
    ShoppingcartService shoppingcartService;

    @Autowired
    MallgoodsService mallgoodsService;

    private final String now = DateUtil.now();

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Shoppingcart cart) {

        if (cart.getId() == null) {
            //goods.setTime(DateUtil.now());
            //goods.setUser(TokenUtils.getCurrentUser().getUsername());
        }

        shoppingcartService.saveOrUpdate(cart);
        return Result.suc();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        shoppingcartService.removeById(id);
        return Result.suc();
    }


    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        shoppingcartService.removeByIds(ids);
        return Result.suc();
    }

    @GetMapping
    public Result findAll() {

        List<Shoppingcart> list = shoppingcartService.list();
        for (Shoppingcart cart : list) {
            Mallgoods goods = mallgoodsService.getById(cart.getGoodid());
            cart.setGoodName(goods.getName());
            cart.setImg(goods.getImg());
            cart.setPrice(goods.getPrice());
        }
        return Result.suc(list);

    }

    @GetMapping("/personCart")
    public Result personCart() {
        BigDecimal total = new BigDecimal(0);
        List<Shoppingcart> list = shoppingcartService.list();
        for (Shoppingcart cart : list) {
            Mallgoods goods = mallgoodsService.getById(cart.getGoodid());
            cart.setGoodName(goods.getName());
            cart.setImg(goods.getImg());
            cart.setPrice(goods.getPrice());
            total = total.add(goods.getPrice().multiply(BigDecimal.valueOf(cart.getNum())));
        }
        Dict dict = Dict.create().set("list", list).set("total", total);
        return Result.suc(dict);
    }

    @PostMapping("/cal")
    public Result cal(@RequestBody List<Shoppingcart> carts) {
        BigDecimal total = new BigDecimal(0);
        for (Shoppingcart cart : carts) {
            Mallgoods goods = mallgoodsService.getById(cart.getGoodid());
            total = total.add(goods.getPrice().multiply(BigDecimal.valueOf(cart.getNum())));
        }
        return Result.suc(total);
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Shoppingcart> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
//        User currentUser = TokenUtils.getCurrentUser();
//        if (currentUser.getRole().equals("ROLE_USER")) {
//            queryWrapper.eq("userid", currentUser.getId());
//        }
        Page<Shoppingcart> page = shoppingcartService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<Shoppingcart> records = page.getRecords();
        for (Shoppingcart record : records) {
            Mallgoods goods = mallgoodsService.getById(record.getGoodid());
            record.setGoodName(goods.getName());
            record.setImg(goods.getImg());
            record.setPrice(goods.getPrice());
        }
        return Result.suc(page);
    }
}
